import sun.reflect.generics.tree.Tree;

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    TreeNode root;
    public boolean search(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }
    public void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode tmp = new TreeNode(val);
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else {
                return;
            }
        }
        if (parent.val > val) {
            parent.left = tmp;
        }else{
            parent.right = tmp;
        }
    }
    public void remove(int val) {
        if(root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else {
                removeNode(parent,cur);
                return;
            }
        }
    }
    //设待删除结点为 cur, 待删除结点的双亲结点为 parent
    //1. cur.left == null
    //  1. cur 是 root，则 root = cur.right
    //  2. cur 不是 root，cur 是 parent.left，则 parent.left = cur.right
    //  3. cur 不是 root，cur 是 parent.right，则 parent.right = cur.right
    //2. cur.right == null

    //  1. cur 是 root，则 root = cur.left
    //  2. cur 不是 root，cur 是 parent.left，则 parent.left = cur.left
    //  3. cur 不是 root，cur 是 parent.right，则 parent.right = cur.left
    //3. cur.left != null && cur.right != null
    //  1.需要使用替换法进行删除，即在它的右子树中寻找中序下的第一个结点(关键码最小)，用它的值填补到被删除节点中，再来处理该结点的删除问题
    //
    public void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                cur = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null) {
            if (cur == root) {
                cur = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
                }
            } else {
            TreeNode tmp = cur.right;
            while (tmp.left != null) {
                parent = tmp;
                tmp = tmp.left;
            }
            cur.val = tmp.val;
            parent.left = tmp.right;
        }
    }
}
